package com.app.micaihu.view.main.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.app.micaihu.R;
import com.app.micaihu.adapter.PagerAdapter.CommonFragmentAdapter;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.BaseOldActivity;
import com.app.micaihu.base.BaseOldFragment;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.ConfigKeyConstant;
import com.app.micaihu.custom.components.downandupload.DownloadUtils;
import com.app.micaihu.manager.AppConfigManager;
import com.app.micaihu.view.main.game.GameDownloadActivity;
import com.app.micaihu.view.main.game.fragment.GameRankingFragment;
import com.app.utils.stickheaderview.tab.SlidingTabLayout;
import com.app.utils.util.osutils.StatusBarUtils;
import com.baidu.mobstat.StatService;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;

/**
 * Created by Guo on 2017/5/22.
 * <p>
 * 排行榜的Fragment 容器
 */

public class RankingContainerFragment extends BaseOldFragment implements ViewPager.OnPageChangeListener, View.OnClickListener {
    private Activity mActivity;
    private ViewGroup mContentView;//根布局
    private ViewPager mViewpager;
    private SlidingTabLayout navTab;

//    private String[] titls = {AppApplication.getInstance().getString(R.string.rank_game),
//            AppApplication.getInstance().getString(R.string.rank_video)
//    };

//    private Handler handler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//
//            switch (msg.what) {
//                case 1:
//                    if (fragments != null && fragments.size() > 1) {
//                        GameRankingFragment fragment = (GameRankingFragment) fragments.get(1);
//                        if (fragment != null) {
//                            fragment.notifyDown();
//                        }
//                    }
//
//
//                    break;
//            }
//
//
//        }
//    };


    private String[] titls;
    private ArrayList<Fragment> fragments;
    private int position = 0;
    private View gameDot;
    private boolean isShowGame;
    private FrameLayout gameBtn;

    @Override
    public void onResume() {
        super.onResume();
//        if (fragments != null && fragments.size() == 2 && position == 1) {
//            flushDownProgress(position);
//        }

        if (gameDot != null) {
            if (DownloadUtils.DOWN_LOAD_COUNT > 0) {
                gameDot.setVisibility(View.VISIBLE);
            } else {
                gameDot.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onAttach(Activity activity) {
        this.mActivity = activity;
        super.onAttach(activity);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mContentView == null) {
            mContentView = (ViewGroup) inflater.inflate(R.layout.fragment_main_rank_page, null);
            StatusBarUtils.setupStatusBarViewInFragmentLinearLayout(getActivity(), mContentView, R.color.app_theme);
            mViewpager = (ViewPager) mContentView.findViewById(R.id.mViewPager);
            navTab = (SlidingTabLayout) mContentView.findViewById(R.id.navig_tab);

            gameBtn = (FrameLayout) mContentView.findViewById(R.id.gameBtn);
            gameDot = mContentView.findViewById(R.id.gameDot);
            gameBtn.setOnClickListener(this);
            initView();
        }
        return mContentView;
    }

    /**
     * eventbus
     *
     * @param parms
     */
    public void onEventMainThread(Bundle parms) {
        if (parms != null) {

            int down = parms.getInt("down");
            if (down == 333 && gameDot != null && DownloadUtils.DOWN_LOAD_COUNT > 0) {
                gameDot.setVisibility(View.VISIBLE);
            }
        }

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    private void initView() {
        fragments = new ArrayList<Fragment>();
        isShowGame = AppConfigManager.getInstance().readConfig(ConfigKeyConstant.isGameShow, false);

        VideoRankingFragment videoRankingFragment = new VideoRankingFragment();
        fragments.add(videoRankingFragment);

        //增加的热点Fragment
        HotNewsFragment hotNewsFragment = new HotNewsFragment();
        fragments.add(hotNewsFragment);

        if (isShowGame) {
            GameRankingFragment gameRankingFragment = new GameRankingFragment();
            Bundle bundle = new Bundle();
            //游戏的混合榜单
            bundle.putString(Config.Extra.PARAMETER1, GameRankingFragment.ALLRANK);
            gameRankingFragment.setArguments(bundle);
            fragments.add(gameRankingFragment);
        }


        if (fragments.size() == 2) {
            titls = new String[2];
            titls[0] = AppApplication.getInstance().getString(R.string.rank_video);
            titls[1] = AppApplication.getInstance().getString(R.string.rank_hot);
            gameBtn.setVisibility(View.GONE);
        } else if (fragments.size() == 3) {
            titls = new String[3];
            titls[0] = AppApplication.getInstance().getString(R.string.rank_video);
            titls[1] = AppApplication.getInstance().getString(R.string.rank_hot);
            titls[2] = AppApplication.getInstance().getString(R.string.rank_game);
            gameBtn.setVisibility(View.GONE);
        }

        if (fragments.size() == titls.length) {
            CommonFragmentAdapter commonFragmentAdapter = new CommonFragmentAdapter(getChildFragmentManager(), fragments, titls);
            mViewpager.setAdapter(commonFragmentAdapter);
            mViewpager.setOffscreenPageLimit(fragments.size());
            navTab.setViewPager(mViewpager);
            mViewpager.setCurrentItem(0);
            navTab.setTabSelected(0);
            mViewpager.addOnPageChangeListener(this);
        }


    }

//    private void flushDownProgress(int position) {
//        if (fragments != null && fragments.size() > position) {
//            handler.sendEmptyMessageDelayed(1, 300);
//        }
//    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        this.position = position;

        flushIcon(position);


//        if (fragments != null && fragments.size() == 2 && position == 1) {
//            flushDownProgress(position);
//        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    @Override
    public void onClick(View v) {
        if (v == null) return;
        switch (v.getId()) {
            case R.id.gameBtn:
                startActivity(new Intent(getContext(), GameDownloadActivity.class));
                StatService.onEvent(getContext(), "063", "左上角我的游戏", 1);//统计事件的点击次数
                break;
        }
    }


    /**
     * 刷新Icon
     *
     * @param position
     */
    private void flushIcon(int position) {
        if (gameBtn != null) {
            if (isShowGame) {
                if (position == 2) {
                    gameBtn.setVisibility(View.VISIBLE);
                } else {
                    gameBtn.setVisibility(View.GONE);
                }
            } else {
                gameBtn.setVisibility(View.GONE);
            }

        }


    }

}
